#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/epoll.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
const char* request = "GET http://index.html HTTP/1.1\r\nConnection: keep-alive\r\n\r\nxxxxxxxxx";

int setNonBlock(int fd)
{
    int old_op = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, old_op | O_NONBLOCK);
    return old_op;
}

void addEvent(int epfd, int sock)
{
    struct epoll_event event;
    event.data.fd = sock;
    event.events = EPOLLOUT | EPOLLET | EPOLLERR;
    assert(epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &event) != -1);
    setNonBlock(sock);
}

bool write_(int sock, const char* buf, int len)
{
    int send_bytes = 0;
    int offset = 0;
    while (true)
    {
        send_bytes = send(sock, buf + offset, len, 0);
        if (send_bytes < 0)
        {
            return false;
        }
        else if (send_bytes == 0)
        {
            return false;
        }
        offset += send_bytes;
        len -= send_bytes;
        if (len <= 0)
        {
            return true;
        }
    }
    return true;
}

bool read_once(int sock, char* buf, int len)
{
    memset(buf, '\0', len);
    int ret = recv(sock, buf, len, 0);
    if (ret < 0)
    {
        return false;
    }
    else if (ret == 0)
    {
        return false;
    }
    printf("sock : %d, read %d bytes of data, content: %s\n", sock, ret, buf);
    return true;
}

void connect_server(int epfd, const char* ip, int port, int num)
{
    struct sockaddr_in address;
    memset(&address, 0, sizeof(address));
    address.sin_family = AF_INET;
    inet_pton(AF_INET, ip, &address.sin_addr);
    address.sin_port = htons(port);
    for (int i = 0;i < num;i++)
    {
        sleep(1);
        int sock = socket(PF_INET, SOCK_STREAM, 0);
        printf("accept 1 sock\n");
        if (sock < 0)
        {
            continue;
        }
        if (connect(sock, (struct sockaddr*)&address, sizeof(address)) < 0)
        {
            printf("connect error\n");
            continue;
        }
        printf("发起一个连接: %d\n", sock);
        addEvent(epfd, sock);
    }
}

void close_conn(int epfd, int sock)
{
    epoll_ctl(epfd, EPOLL_CTL_DEL, sock, NULL);
    close(sock);
}

int main(int argc, char* argv[])
{
    if (argc != 4)
    {
        printf("Usage: %s ip port num", argv[0]);
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi(argv[2]);
    //连接请求的数量
    int num = atoi(argv[3]);

    //创建epoll模型
    int epfd = epoll_create(100);
    assert(epfd != -1);
    connect_server(epfd, ip, port, num);
    struct epoll_event events[10000];
    char buf[2048];
    while (true)
    {
        int n = epoll_wait(epfd, events, 10000, 2000);
        for (int i = 0;i < n;i++)
        {
            int confd = events[i].data.fd;
            if (events[i].events & EPOLLIN)
            {
                if (!read_once(confd, buf, 2048))
                {
                    close_conn(epfd, confd);
                }
                struct epoll_event event;
                event.data.fd = confd;
                event.events = EPOLLET | EPOLLERR | EPOLLOUT;
                epoll_ctl(epfd, EPOLL_CTL_MOD, confd, &event);
            }
            else if (events[i].events & EPOLLOUT)
            {
                if (!write_(confd, request, strlen(request)))
                {
                    close_conn(epfd, confd);
                }
                struct epoll_event event;
                event.data.fd = confd;
                event.events = EPOLLET | EPOLLERR | EPOLLIN;
                epoll_ctl(epfd, EPOLL_CTL_MOD, confd, &event);
            }
            else if (events[i].events & EPOLLERR)
            {
                close_conn(epfd, confd);
            }
        }
    }
    close(epfd);

    return 0;
}